<?php 
define('IN_APP', 1); // 定义 IN_APP ,表示是正常入口进入. 后面的文件里都定义了见不到 IN_APP 就 die
define('GROUP_NAME','')   ;
//define('G_MYSQL_DNS','mysql:host=115.238.55.51;port=3308;dbname=wd'); define('G_MYSQL_IPPORT','115.238.55.51:3308');define('OODEBUG',1);  // 机房服务器
// ~E_ALL 的 ~ 去除 即可直接在客户端显示错误
// 否则错误输出到 log 目录下的文件里
error_reporting(~E_ALL);
define('OOAPP_DIRNAME', 'web_admin');
define('OO_JUMP', 1); // 定义了这个 那么就可以跳过 thinkphp 直接执行到后面代码,而不用管什么控制器

// thinkphp 的设置和加载
define('APP_DEBUG', true);

define('APP_NAME', ''); // 定义项目名称
define('WWW_PATH', str_replace("\\", '/', dirname(__FILE__))); //当前项目路径
define('APP_PATH', dirname(dirname(__FILE__)) . '/' . OOAPP_DIRNAME . '/'); //定义项目路径

//define('MODE_NAME','cli'); // 设置为命令模式
$_set = dirname(__FILE__).'/ind/js/set.php';   // $_GINFO 设置在 set.php 中
if(file_exists($_set)) require $_set;
// define('MODE_NAME','cli'); // 设置为命令模式
require dirname(dirname(__FILE__)) . '/' . OOAPP_DIRNAME . '/INC.php'; // INC.php 框架模型必须具备的类,不管什么项目都需要加载

require dirname(dirname(__FILE__)) . '/ThinkPHP/ThinkPHP.php';

set_time_limit(0); // 不限制执行时间
 

$step=isset($_GET['step'])?intval($_GET['step']):0;//默认转让用户数据
$page=isset($_GET['page'])?intval($_GET['page']):1;

 
 



/**数据转换start**/

if($step==0){
        
	echo "
报表统计说明
------------------------------------------------------------------------------------------------
 <a href=?step=".($step+1)."&t=".mt_rand(1,1000).">开始转换>>></a>

	";
    exit;
}

//第1步 转换 充值
elseif($step==1){// 
        $pagesize=500;//每次执行10条记录
        $index = $pagesize * ($page - 1);

  

        $total=Db::G()->GetNum("SELECT count(*) FROM  04a_3applyqueue   where   (aptype =7 or aptype=4 ) and   status=3    ");
		$max_page= intval($total/$pagesize)+1;
        if($page>$max_page) {
			//echo "<script>window.location.href='?step=".($step+1).'; </script>";
			echo "充值报表:第1步执行完成！<a href=?step=".($step+1)."&t=".mt_rand(1,1000).">执行下一次命令</a>";
			//sleep(10);
			echo "<script>window.location.href='?step=".($step+1)."&t=".mt_rand(1,1000)."'; </script>";
			exit;
		}
	    $rows = Db::G()->GetRows("SELECT * FROM  04a_3applyqueue   where  (aptype =7 or aptype=4 ) and   status=3   order by  apid asc limit $index, $pagesize"); // 每次处理 100 行
		foreach($rows as &$row){   
             //写入到报表里：
			 $c=Db::G()->GetRow("SELECT  cid from   01u_0base    where  uid='".$row['uid']."' ");
			 //print_r($row);
			 //echo "<br>";
			 if($c['cid']>1){
			   echo $row['uid']."-".$c['cid']."-".date("Y-n-j",strtotime($row['checktime']))."<br>";
			 }
			 if($row['aptype']==7){//线上tot_cashnet
			 CTLog::G()->doctAndDayReal('uid',$row['uid'],'tot_cashnet',$row['money'],date("Y",strtotime($row['checktime'])),date("n",strtotime($row['checktime'])),date("j",strtotime($row['checktime'])));
             CTLog::G()->doctAndDayReal('st01',1,'tot_cashnet',$row['money'],date("Y",strtotime($row['checktime'])),date("n",strtotime($row['checktime'])),date("j",strtotime($row['checktime'])));  
             CTLog::G()->doctAndDayCidReal("cid",($c['cid']?$c['cid']:1),"tot_cashnet",$row['money'],date("Y",strtotime($row['checktime'])),date("n",strtotime($row['checktime'])),date("j",strtotime($row['checktime'])) );
			 }else{//线下tot_cashbnet
			  CTLog::G()->doctAndDayReal('uid',$row['uid'],'tot_cashbnet',$row['money'],date("Y",strtotime($row['checktime'])),date("n",strtotime($row['checktime'])),date("j",strtotime($row['checktime'])));
             CTLog::G()->doctAndDayReal('st01',1,'tot_cashbnet',$row['money'],date("Y",strtotime($row['checktime'])),date("n",strtotime($row['checktime'])),date("j",strtotime($row['checktime'])));  
             CTLog::G()->doctAndDayCidReal("cid",($c['cid']?$c['cid']:1),"tot_cashbnet",$row['money'],date("Y",strtotime($row['checktime'])),date("n",strtotime($row['checktime'])),date("j",strtotime($row['checktime'])) );
			 }
			 if($c['cid']>0) 	 {
				Db::G()->Exec("update  04a_3applyqueue   set  cid='".$c['cid']."'   uid='".$row['uid']."' ");
			 }
		}
		echo "第1步 充值报表:正在执行第".$page."/".$max_page."页,每次执行:".$pagesize.",总共".$total.",<a href=?step=".$step."&page=".($page+1).">即将执行下一页：".($page+1)."</a>";
		//sleep(1);
		echo "<script>window.location.href='?step=".$step."&page=".($page+1)."'; </script>";
		exit;



}


 
elseif($step==2){//用户信息处理 2013-9-3    ok 
        $pagesize=1000;//每次执行10条记录
        $index = $pagesize * ($page - 1);

  

        $total=Db::G()->GetNum("SELECT count(*) FROM  04a_3applyqueue   where   (aptype =7 or aptype=4 ) and   status=3    ");
		$max_page= intval($total/$pagesize)+1;
        if($page>$max_page) {
			//echo "<script>window.location.href='?step=".($step+1).'; </script>";
			echo "提现统计:第2步执行完成！<a href=?step=".($step+1)."&t=".mt_rand(1,1000).">执行下一次命令</a>";
			//sleep(10);
			echo "<script>window.location.href='?step=".($step+1)."&t=".mt_rand(1,1000)."'; </script>";
			exit;
		}
	    $rows = Db::G()->GetRows("SELECT * FROM  04a_3applyqueue   where   aptype =6   and   status=3   order by  apid asc limit $index, $pagesize"); // 每次处理 100 行
		foreach($rows as &$row){   
             //写入到报表里：
                   $c=Db::G()->GetRow("SELECT  cid from   01u_0base    where  uid='".$row['uid']."' ");
					if(Db::G()->GetRow("SELECT  uid from  01u_2borrower   where  uid='".$row['uid']."' ")){//借款人
							CTLog::G()->doctAndDayReal('uid',$row['uid'],'tot_cashout_b',$row['money'],date("Y",strtotime($row['checktime'])),date("n",strtotime($row['checktime'])),date("j",strtotime($row['checktime']))    );
							CTLog::G()->doctAndDayReal('st01',1,'tot_cashout_b',$row['money'],date("Y",strtotime($row['checktime'])),date("n",strtotime($row['checktime'])),date("j",strtotime($row['checktime']))  );  
						    CTLog::G()->doctAndDayCidReal("cid",($c['cid']?$c['cid']:1),"tot_cashout_b",$row['money'],date("Y",strtotime($row['checktime'])),date("n",strtotime($row['checktime'])),date("j",strtotime($row['checktime']))  );
					}else{//投资人2014-2-7

					CTLog::G()->doctAndDayReal('uid',$row['uid'],'tot_cashout_t',$row['money'],date("Y",strtotime($row['checktime'])),date("n",strtotime($row['checktime'])),date("j",strtotime($row['checktime']))   );
					CTLog::G()->doctAndDayReal('st01',1,'tot_cashout_t',$row['money'],date("Y",strtotime($row['checktime'])),date("n",strtotime($row['checktime'])),date("j",strtotime($row['checktime']))   );  
					 CTLog::G()->doctAndDayCidReal("cid",($c['cid']?$c['cid']:1),"tot_cashout_t",$row['money'],date("Y",strtotime($row['checktime'])),date("n",strtotime($row['checktime'])),date("j",strtotime($row['checktime']))   );
					}

								 if($c['cid']>0) 	 {
				Db::G()->Exec("update  04a_3applyqueue   set  cid='".$c['cid']."'   uid='".$row['uid']."' ");
			 }


		}
		echo "第2步 提现统计:正在执行第".$page."/".$max_page."页,每次执行:".$pagesize.",总共".$total.",<a href=?step=".$step."&page=".($page+1).">即将执行下一页：".($page+1)."</a>";
		//sleep(1);
		echo "<script>window.location.href='?step=".$step."&page=".($page+1)."'; </script>";
		exit;



}

 
 
elseif($step==3){//用户信息处理 2013-9-3    ok 
        $pagesize=500;//每次执行10条记录
        $index = $pagesize * ($page - 1);

  

        $total=Db::G()->GetNum("SELECT count(*) FROM  05b_0base    where     status in(3,6)    ");
		$max_page= intval($total/$pagesize)+1;
        if($page>$max_page) {
			//echo "<script>window.location.href='?step=".($step+1).'; </script>";
			echo "发标额逾期等的统计:第3步执行完成！<a href=?step=".($step+1)."&t=".mt_rand(1,1000).">执行下一次命令</a>";
			//sleep(10);
			echo "<script>window.location.href='?step=".($step+1)."&t=".mt_rand(1,1000)."'; </script>";
			exit;
		}
	    $rows = Db::G()->GetRows("SELECT * FROM  05b_0base   where     status in(3,6)   order by  bid asc limit $index, $pagesize"); // 每次处理 100 行
		foreach($rows as &$row){   
			//echo $row['account']."-".date("Y",strtotime($row['reverify_time']))."-".date("n",strtotime($row['reverify_time']))."-".date("j",strtotime($row['reverify_time']))."<br><br>";
			//echo "1<br><br>";
             //写入到报表里：
            $c=Db::G()->GetRow("SELECT  cid from   01u_0base    where  uid='".$row['uid']."' ");
			CTLog::G()->doctAndDayReal('st01',1,'tot_b_account',$row['account'],date("Y",strtotime($row['reverify_time'])),date("n",strtotime($row['reverify_time'])),date("j",strtotime($row['reverify_time']))  );   //网站统计信息 借款总额
			CTLog::G()->doctAndDayCidReal("cid",($c['cid']?$c['cid']:1),"tot_b_account",$row['account'],date("Y",strtotime($row['reverify_time'])),date("n",strtotime($row['reverify_time'])),date("j",strtotime($row['reverify_time']))  );//分站的借款zong'e

            //找出还款的相关明细
			$rs =array();
            $rs = Db::G()->GetRows("SELECT * FROM  05b_2list_repay    where   bid='".$row['bid']."'  order by  id asc ");
			foreach($rs as &$r){   
               echo $row['bid'];
			   echo "<br>--<br>";
			   CTLog::G()->doctAndDayReal('cid',$c['cid'],'mon_moneyyhall',$r['repay_account'],date("Y",strtotime($r['repay_time'])),date("n",strtotime($r['repay_time'])),date("j",strtotime($r['repay_time'])) ); // 统计待收款
			}

		}
		echo "第3步 发标额逾期等的统计:正在执行第".$page."/".$max_page."页,每次执行:".$pagesize.",总共".$total.",<a href=?step=".$step."&page=".($page+1).">即将执行下一页：".($page+1)."</a>";
		//sleep(1);
		echo "<script>window.location.href='?step=".$step."&page=".($page+1)."'; </script>";
		exit;



}


 
elseif($step==4){// 已还信息
        $pagesize=500;//每次执行10条记录
        $index = $pagesize * ($page - 1);

  

        $total=Db::G()->GetNum("SELECT count(*) FROM  05b_0base    where      status in(3,6)    ");
		$max_page= intval($total/$pagesize)+1;
        if($page>$max_page) {
			//echo "<script>window.location.href='?step=".($step+1).'; </script>";
			echo "已收款 的统计:第4步执行完成！<a href=?step=".($step+1)."&t=".mt_rand(1,1000).">执行下一次命令</a>";
			//sleep(10);
			echo "<script>window.location.href='?step=".($step+1)."&t=".mt_rand(1,1000)."'; </script>";
			exit;
		}
	    $rows = Db::G()->GetRows("SELECT * FROM  05b_0base   where     status in(3,6)   order by  bid asc limit $index, $pagesize"); // 每次处理 100 行
		foreach($rows as &$row){  
			            $c=Db::G()->GetRow("SELECT  cid from   01u_0base    where  uid='".$row['uid']."' ");
  echo $row['bid'];
			   echo "<br><br>";
            //找出还款的相关明细
			$rs =array();
            $rs = Db::G()->GetRows("SELECT * FROM  05b_2list_repay    where   bid='".$row['bid']."'   and   repay_status=1 and repay_web=0   order by  id asc ");//没有垫付的
			foreach($rs as &$r){   
	 
CTLog::G()->doctAndDayReal('st01',1,'tc_account',$r['repay_account_yes'],date("Y",strtotime($r['repay_yestime'])),date("n",strtotime($r['repay_yestime'])),date("j",strtotime($r['repay_yestime'])));//总的还款费用
CTLog::G()->doctAndDayReal('st01',1,'tc_capital',$r['repay_capital_yes'],date("Y",strtotime($r['repay_yestime'])),date("n",strtotime($r['repay_yestime'])),date("j",strtotime($r['repay_yestime'])) );//总的还款本金
CTLog::G()->doctAndDayReal('st01',1,'tc_interest',$r['repay_interest_yes'],date("Y",strtotime($r['repay_yestime'])),date("n",strtotime($r['repay_yestime'])),date("j",strtotime($r['repay_yestime'])) );//总的还款利息

CTLog::G()->doctAndDayCidReal("cid",$c['cid'],"tc_account",$r['repay_account_yes'],date("Y",strtotime($r['repay_yestime'])),date("n",strtotime($r['repay_yestime'])),date("j",strtotime($r['repay_yestime'])) ); //分站统计    
CTLog::G()->doctAndDayCidReal("cid",$c['cid'],"tc_capital",$r['repay_capital_yes'],date("Y",strtotime($r['repay_yestime'])),date("n",strtotime($r['repay_yestime'])),date("j",strtotime($r['repay_yestime'])) ); //分站统计   
CTLog::G()->doctAndDayCidReal("cid",$c['cid'],"tc_interest",$r['repay_interest_yes'],date("Y",strtotime($r['repay_yestime'])),date("n",strtotime($r['repay_yestime'])),date("j",strtotime($r['repay_yestime'])) ); //分站统计  
 			}

		}
		echo "第4步 已收款 的统计:正在执行第".$page."/".$max_page."页,每次执行:".$pagesize.",总共".$total.",<a href=?step=".$step."&page=".($page+1).">即将执行下一页：".($page+1)."</a>";
		//sleep(1);
		echo "<script>window.location.href='?step=".$step."&page=".($page+1)."'; </script>";
		exit;



}


 
elseif($step==5){// 已还信息
        $pagesize=500;//每次执行10条记录
        $index = $pagesize * ($page - 1);

  

        $total=Db::G()->GetNum("SELECT count(*) FROM  05b_0base    where      status in(3,6)    ");
		$max_page= intval($total/$pagesize)+1;
        if($page>$max_page) {
			//echo "<script>window.location.href='?step=".($step+1).'; </script>";
			echo "已垫付 的统计:第5步执行完成！<a href=?step=".($step+1)."&t=".mt_rand(1,1000).">执行下一次命令</a>";
			//sleep(10);
			echo "<script>window.location.href='?step=".($step+1)."&t=".mt_rand(1,1000)."'; </script>";
			exit;
		}
	    $rows = Db::G()->GetRows("SELECT * FROM  05b_0base   where    status in(3,6)    order by  bid asc limit $index, $pagesize"); // 每次处理 100 行
		foreach($rows as &$row){   
            //找出还款的相关明细
			$c=Db::G()->GetRow("SELECT  cid from   01u_0base    where  uid='".$row['uid']."' ");
			$rs =array();
			  echo $row['bid'];
			   echo "<br><br>";
            $rs = Db::G()->GetRows("SELECT * FROM  05b_2list_repay    where    bid='".$row['bid']."'  and  repay_period='".$row['borrow_period']."' and  repay_web=1   order by  id asc ");//已垫付
				foreach($rs as &$r){   
					CTLog::G()->doctAndDayReal('st01',1,'tot_df_account',$r['repay_account'],date("Y",strtotime($r['dianfu_time'])),date("n",strtotime($r['dianfu_time'])),date("j",strtotime($r['dianfu_time']))  );  // 网站统计信息 垫付总额
					CTLog::G()->doctAndDayReal('st01',1,'tot_df_capital',$r['repay_capital'],date("Y",strtotime($r['dianfu_time'])),date("n",strtotime($r['dianfu_time'])),date("j",strtotime($r['dianfu_time']))  );// 垫付本金  

					CTLog::G()->doctAndDayReal('st01',1,'tot_df_interest',$r['repay_interest'],date("Y",strtotime($r['dianfu_time'])),date("n",strtotime($r['dianfu_time'])),date("j",strtotime($r['dianfu_time']))  );// 垫付本金 

					CTLog::G()->doctAndDayReal('st01',1,'tot_df_other',0,date("Y",strtotime($r['dianfu_time'])),date("n",strtotime($r['dianfu_time'])),date("j",strtotime($r['dianfu_time']))  );       // 垫付其他手续费  
					CTLog::G()->doctAndDayCidReal("cid",$c['cid'],"tot_df_account",$r['repay_account'],date("Y",strtotime($r['dianfu_time'])),date("n",strtotime($r['dianfu_time'])),date("j",strtotime($r['dianfu_time'])) ); //分站统计   垫付总额
					CTLog::G()->doctAndDayCidReal("cid",$c['cid'],"tot_df_capital",$r['repay_capital'],date("Y",strtotime($r['dianfu_time'])),date("n",strtotime($r['dianfu_time'])),date("j",strtotime($r['dianfu_time'])) ); //分站统计   垫付总额

					CTLog::G()->doctAndDayCidReal("cid",$c['cid'],"tot_df_interest",$r['repay_interest'],date("Y",strtotime($r['dianfu_time'])),date("n",strtotime($r['dianfu_time'])),date("j",strtotime($r['dianfu_time'])) );

					CTLog::G()->doctAndDayCidReal("cid",$c['cid'],"tot_df_other",0,date("Y",strtotime($r['dianfu_time'])),date("n",strtotime($r['dianfu_time'])),date("j",strtotime($r['dianfu_time'])) ); //分站统计   垫付总额 
 				}

		}
		echo "第5步 已垫付 的统计:正在执行第".$page."/".$max_page."页,每次执行:".$pagesize.",总共".$total.",<a href=?step=".$step."&page=".($page+1).">即将执行下一页：".($page+1)."</a>";
		//sleep(1);
		echo "<script>window.location.href='?step=".$step."&page=".($page+1)."'; </script>";
		exit;



}


else{
	  
 
      exit("数据报表 转换ok");
}


